package vn.edu.hut.soict.translate.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import vn.hus.nlp.tokenizer.DefaultTokenizerFactory;
import vn.hus.nlp.tokenizer.Token;
import vn.hus.nlp.tokenizer.Tokenizer;

public class StringUtils {
	private static final String DEFAULT_RESOURCE_DIRECTORY = "res";
	private static DefaultTokenizerFactory tokenizerFactory;

	public static List<String> splitIntoSyllable(String string) {
		if (string == null) {
			return null;
		}
		String[] syllables = string.split("\\s+");
		List<String> syllableList = new ArrayList<String>();
		for (String syllable : syllables) {
			syllableList.add(syllable);
		}
		return syllableList;
	}

	public static List<String> splitIntoToken(String string) {
		if (tokenizerFactory == null) {
			try {
				tokenizerFactory = new DefaultTokenizerFactory(DEFAULT_RESOURCE_DIRECTORY);
			}
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (string == null) {
			return null;
		}
		Tokenizer tokenizer = tokenizerFactory.getTokenizer(string.toLowerCase());
		List<String> tokens = new ArrayList<String>();
		while (tokenizer.hasNext()) {
			Token token = tokenizer.next();
			String text = token.getText();
			tokens.add(text);
		}
		return tokens;
	}

	public static List<String> parseToken(String string) {
		if (string == null) {
			return null;
		}
		String[] tokens = string.split("\\s+");
		for (int i = 0; i < tokens.length; i++) {
			tokens[i] = tokens[i].replace("_", " ");
		}
		return Arrays.asList(tokens);
	}

	public static int syllableCount(String string) {
		if (string == null) {
			return 0;
		}
		String[] syllables = string.split("\\s+");
		return syllables.length;
	}

	public static String addExtraSpace(String s) {
		if (s == null) {
			return null;
		}
		String regex1 = "([\\(\\[\\{\"])(\\S)";
		String regex2 = "(\\S)([\\)\\]\\}\";:!?])";
		String regex3 = "([^\\p{Lu}])(\\.,)";
		return s.replaceAll(regex1, "$1 $2").replaceAll(regex2, "$1 $2").replaceAll(regex3, "$1 $2");
	}
}
